package com.summer.learn.converters;

import com.summer.learn.dao.DepartmentDao;
import com.summer.learn.modle.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class EmployeeConverter implements Converter<String, Employee> {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public Employee convert(String source) {
        // lastname-email-gender-department.id例如:GG-gg@atguigu.com-0-105
        String[] split = source.split("-");
        if(split != null && split.length == 4) {
            Employee employee = new Employee();
            employee.setLastName(split[0]);
            employee.setEmail(split[1]);
            employee.setGender(Integer.valueOf(split[2]));
            employee.setDepartment(departmentDao.getDepartment(Integer.valueOf(split[3])));
            System.out.println(source + "-- to --" + employee);
            return employee;
        }
        return null;
    }

    @Override
    public <U> Converter<String, U> andThen(Converter<? super Employee, ? extends U> after) {
        return Converter.super.andThen(after);
    }

}
